package com.huawei.hms.framework.network.restclient.hwhttp.dns.dnresolver;

import com.huawei.hms.framework.common.Logger;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.DNManager;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.DnsUtil;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.dnresolver.dnkeeper.DomainResult;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

public class DnsCallable implements Callable {
    private static final int DNKEEPER_TIMEOUT = 10000;
    private static final int LOOKUP_TIMEOUT = 2000;
    private static final String TAG = "DnsCallable";
    private String domain;
    private Future<DomainResult> futureLocal = null;
    private Future<DomainResult> futurednkeeper = null;
    private ExecutorService lookupThreadPool;

    class DnkeeperCallable implements Callable {
        private String domain;

        public DnkeeperCallable(String str) {
            this.domain = str;
        }

        public DomainResult call() {
            Logger.v(DnsCallable.TAG, "dnkeeperCallable call");
            DomainResult lookup = Dns.DNKEEPER.lookup(this.domain);
            if (!DnsUtil.isIpListEmpty(lookup)) {
                DnsCallable.this.futureLocal.cancel(true);
            }
            return lookup;
        }
    }

    class LocalCallable implements Callable {
        private String domain;

        public LocalCallable(String str) {
            this.domain = str;
        }

        public DomainResult call() {
            Logger.v(DnsCallable.TAG, "localCallable call");
            DomainResult lookup = Dns.LOCAL_DNS.lookup(this.domain);
            if (!(DnsUtil.isIpListEmpty(lookup) || DnsCallable.this.futurednkeeper == null)) {
                DnsCallable.this.futurednkeeper.cancel(true);
            }
            return lookup;
        }
    }

    public DnsCallable(String str, ExecutorService executorService) {
        this.domain = str;
        this.lookupThreadPool = executorService;
    }

    public DomainResult call() {
        Logger.v(TAG, "DnsCallable call");
        DomainResult domainResult = new DomainResult();
        this.futureLocal = this.lookupThreadPool.submit(new LocalCallable(this.domain));
        try {
            Logger.i(TAG, "query from local domain: " + this.domain);
            DNManager.getInstance().setResolverSource(this.domain, 4);
            domainResult = (DomainResult) this.futureLocal.get(2000, TimeUnit.MILLISECONDS);
        } catch (Throwable e) {
            Logger.w(TAG, "query failed LOOKUP_TIMEOUT", e);
        }
        if (DnsUtil.isIpListEmpty(domainResult)) {
            Logger.i(TAG, "query from dnkeeper domain: " + this.domain);
            this.futurednkeeper = this.lookupThreadPool.submit(new DnkeeperCallable(this.domain));
            try {
                DNManager.getInstance().setResolverSource(this.domain, 5);
                domainResult = (DomainResult) this.futurednkeeper.get(10000, TimeUnit.MILLISECONDS);
            } catch (Throwable e2) {
                Logger.w(TAG, "query from dnkeeper failed", e2);
            }
        }
        if (!DnsUtil.isIpListEmpty(domainResult)) {
            return domainResult;
        }
        DomainResult domainResult2;
        DNManager.getInstance().setResolverSource(this.domain, 4);
        try {
            domainResult2 = (DomainResult) this.futureLocal.get((long) DnsUtil.getDnstime(), TimeUnit.MILLISECONDS);
        } catch (Throwable e22) {
            Logger.w(TAG, "query failed CANCEL_TIMEOUT", e22);
            this.futureLocal.cancel(true);
            domainResult2 = domainResult;
        }
        if (this.futurednkeeper == null) {
            return domainResult2;
        }
        this.futurednkeeper.cancel(true);
        return domainResult2;
    }
}
